package marmalius.craft;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import marmalius.Player;
import marmalius.client;
import marmalius.item.ItemService;
import marmalius.player.SkillType;
import marmalius.tools.DBService;
import marmalius.tools.LogService;

public class SmithingService {
	private static final SmithingService instance = new SmithingService();
	private Map<Integer, SmithingPos> smithingPos;
	private Map<Integer, SmithingItem> smithingItem;
	private Map<Integer, List<Integer>> barID; //use ram but faster;
	
	public SmithingService() {
		smithingPos = new HashMap<Integer, SmithingPos>();
		smithingItem = new HashMap<Integer, SmithingItem>();
		barID = new HashMap<Integer, List<Integer>>();
		loadSmithingPos();
	}
	
	private void loadSmithingPos() {
		smithingPos.put(1, new SmithingPos(0, 1119, 1094, 1125));
		smithingPos.put(2, new SmithingPos(0, 1120, 1091, 1126));
		smithingPos.put(3, new SmithingPos(0, 1121, 1098, 1109));
		smithingPos.put(4, new SmithingPos(0, 1122, 1102, 1127));
		smithingPos.put(5, new SmithingPos(0, 1123, 1107, 1128));
		smithingPos.put(11, new SmithingPos(1, 1119, 1085, 1124));
		smithingPos.put(12, new SmithingPos(1, 1120, 1093, 1129));
		smithingPos.put(13, new SmithingPos(1, 1121, 1099, 1110));
		smithingPos.put(14, new SmithingPos(1, 1122, 1103, 1113));
		smithingPos.put(15, new SmithingPos(1, 1123, 1108, 1130));
		smithingPos.put(21, new SmithingPos(2, 1119, 1087, 1116));
		smithingPos.put(22, new SmithingPos(2, 1120, 1083, 1118));
		smithingPos.put(23, new SmithingPos(2, 1121, 1100, 1111));
		smithingPos.put(24, new SmithingPos(2, 1122, 1104, 1114));
		smithingPos.put(25, new SmithingPos(2, 1123, 1106, 1131));
		smithingPos.put(31, new SmithingPos(3, 1119, 1086, 1089));
		smithingPos.put(32, new SmithingPos(3, 1120, 1092, 1095));
		smithingPos.put(33, new SmithingPos(3, 1121, 1101, 1112));
		smithingPos.put(34, new SmithingPos(3, 1122, 1105, 1115));
		smithingPos.put(35, new SmithingPos(3, 1123, 1096, 1132));
		smithingPos.put(41, new SmithingPos(4, 1119, 1088, 1090));
		smithingPos.put(42, new SmithingPos(4, 1120, 8429, 8428));
		smithingPos.put(43, new SmithingPos(4, 1121, 11461, 11459));
		smithingPos.put(44, new SmithingPos(4, 1122, 13358, 13357));
		smithingPos.put(45, new SmithingPos(4, 1123, 1134, 1135));
	}
	
	public boolean loadSmithing() {
		smithingItem.clear();
		int nb = 0;
		try {
			ResultSet rs = DBService.getInstance().doQuery("SELECT * FROM craft_smithing ORDER BY position ASC");
			while (rs.next()) {
				smithingItem.put(rs.getInt("item"), new SmithingItem(rs.getInt("reqbar"), rs.getInt("reqcount"), rs.getInt("reqlevel"), rs.getInt("item"), rs.getString("name"), rs.getInt("count"), rs.getInt("position")));
				if (!barID.containsKey(rs.getInt("reqbar")))
					barID.put(rs.getInt("reqbar"), new ArrayList<Integer>());
				
				barID.get(rs.getInt("reqbar")).add(rs.getInt("item"));
				
				nb++;
			}
			rs.close();
			LogService.getInstance().logInfo("craft_smithing loaded ("+nb+")");
			return true;
		}
		catch (SQLException ex) {
			LogService.getInstance().logError("can't load craft_smithing");
			LogService.getInstance().logError(ex.getMessage());
		}
		return false;
	}
	
	public static boolean isAnvil(int objectID) {
		return objectID == 2783;
	}
	
	public void getSmithingInterface(Player player, int itemID) {
		switch (itemID) {
			case 2349: //Bronze Bar
			case 2351: //Iron Bar
			case 2353: //Steel Bar
			case 2359: //Mithril Bar
			case 2361: //Adamantite Bar
			case 2363: //Runite Bar
				initSmithing2(player, itemID);
				((client)player).flushOutStream();
		}
	}
	
	private void initSmithing2(Player player, int barType) {
		client client = (client)player;
		int barCount = player.getInventory().getItemCount(barType);
		client.outStream.createFrame(97);
		client.outStream.writeWord(994);
		List<Integer> items = barID.get(barType);
		List<Integer> leftPos = getAllPos();
		
		if (items == null)
			return ;
		for (int itemID : items) {
			SmithingItem sItem = smithingItem.get(itemID);
			if (sItem == null)
				continue;
			SmithingPos pos = smithingPos.get(sItem.getPosition());
			if (pos == null)
				continue;
			if (barCount < sItem.getReqCount()) {
				if (sItem.getReqCount() > 1)
					client.sendQuest("@red@"+sItem.getReqCount()+"bars@red@", pos.getBar());
				else
					client.sendQuest("@red@1bar@red@", pos.getBar());
			}
			else {
				if (sItem.getReqCount() > 1)
					client.sendQuest("@gre@"+sItem.getReqCount()+"bars@gre@", pos.getBar());
				else
					client.sendQuest("@gre@1bar@gre@", pos.getBar());
			}
			if (player.getSkillLevel(SkillType.SMITHING) < sItem.getReqLevel())
				client.sendQuest("@bla@"+sItem.getName()+"@bla@", pos.getName());
			else
				client.sendQuest("@whi@"+sItem.getName()+"@whi@", pos.getName());
			client.addItemToSmith(sItem.getItem(), pos.getSlot(), pos.getColumn(), sItem.getCount());
			leftPos.remove((Object)sItem.getPosition());
		}
		
		for (int posID : leftPos) {
			SmithingPos pos = smithingPos.get(posID);
			if (pos == null)
				continue;
			client.sendQuest("", pos.getBar());
			client.sendQuest("", pos.getName());
		}
	}
	
	private List<Integer> getAllPos() {
		List<Integer> allPos = new ArrayList<Integer>();
		for (int i = 1; i <= 5; i++)
			allPos.add(i);
		for (int i = 11; i <= 15; i++)
			allPos.add(i);
		for (int i = 21; i <= 25; i++)
			allPos.add(i);
		for (int i = 31; i <= 35; i++)
			allPos.add(i);
		for (int i = 41; i <= 45; i++)
			allPos.add(i);
		return allPos;
	}
	
	public void makeSmithingItem(Player player, int itemID, int factor) {
		SmithingItem item = smithingItem.get(itemID);
		if (item == null)
			return ;
		if (item.getReqLevel() > player.getSkillLevel(SkillType.SMITHING)) {
			player.sendPlayerMessage("You need a higher smithing level to smith "+ItemService.getInstance().getItemTemplate(itemID).getName()+"s");
			return ;
		}
		if (!player.getInventory().hasItemCount(item.getReqBar(), item.getReqCount())) {
			player.sendPlayerMessage("You dont have enough bars to make this");
			return ;
		}
		if (!player.getInventory().haveSpaceForItem(itemID, item.getCount())) {
			//TODO message //vous n'avez pas la place necessaire
			return ;
		}
		if (!player.getInventory().hasItem(2347)) {
			player.sendPlayerMessage("You need a hammer to smith this item.");
			return ;
		}
		((client)player).RemoveAllWindows();
		player.startAnimation(898);
		player.getInventory().removeItem(item.getReqBar(), item.getReqCount());
		player.getInventory().addItem(itemID, item.getCount());
		player.sendPlayerMessage("?"); //TODO bad message
		((client)player).addSkillXP(1, SkillType.SMITHING.getValue()); //TODO bad count XP
	}
	
	/*
	private void initSmithing(Player player, int barType) {
		client client = (client)player;
		int barCount = player.getInventory().getItemCount(barType);
		client.outStream.createFrame(97);
		client.outStream.writeWord(994);
		if (barCount < 5)
			client.sendQuest("@red@1112@red@", 1112);
		else
			client.sendQuest("@gre@1112@gre@", 1112);
		if (barCount < 3) {
			client.sendQuest("@red@1109@red@", 1109);
			client.sendQuest("@red@1110@red@", 1110);
			client.sendQuest("@red@1118@red@", 1118);
			client.sendQuest("@red@1111@red@", 1111);
			client.sendQuest("@red@1095@red@", 1095);
			client.sendQuest("@red@1115@red@", 1115);
			client.sendQuest("@red@1090@red@", 1090);
		} 
		else {
			client.sendQuest("@gre@1109@gre@", 1109);
			client.sendQuest("@gre@1110@gre@", 1110);
			client.sendQuest("@gre@1118@gre@", 1118);
			client.sendQuest("@gre@1111@gre@", 1111);
			client.sendQuest("@gre@1095@gre@", 1095);
			client.sendQuest("@gre@1115@gre@", 1115);
			client.sendQuest("@gre@1090@gre@", 1090);
		}
		if (barCount < 2) {
			client.sendQuest("@red@1113@red@", 1113);
			client.sendQuest("@red@1116@red@", 1116);
			client.sendQuest("@red@1114@red@", 1114);
			client.sendQuest("@red@1089@red@", 1089);
			client.sendQuest("@red@8428@red@", 8428);
		}
		else {
			client.sendQuest("@gre@1113@gre@", 1113);
			client.sendQuest("@gre@1116@gre@", 1116);
			client.sendQuest("@gre@1114@gre@", 1114);
			client.sendQuest("@gre@1089@gre@", 1089);
			client.sendQuest("@gre@8428@gre@", 8428);
		}
		if (barCount < 1) {
			client.sendQuest("@red@1125@red@", 1125);
			client.sendQuest("@red@1126@red@", 1126);
			client.sendQuest("@red@1127@red@", 1127);
			client.sendQuest("@red@1124@red@", 1124);
			client.sendQuest("@red@1128@red@", 1128);
			client.sendQuest("@red@1129@red@", 1129);
			client.sendQuest("@red@1130@red@", 1130);
			client.sendQuest("@red@13357@red@", 13357);
			client.sendQuest("@red@1131@red@", 1131);
			client.sendQuest("@red@11459@red@", 11459);
		}
		else {
			client.sendQuest("@gre@1125@gre@", 1125);
			client.sendQuest("@gre@1126@gre@", 1126);
			client.sendQuest("@gre@1127@gre@", 1127);
			client.sendQuest("@gre@1124@gre@", 1124);
			client.sendQuest("@gre@1128@gre@", 1128);
			client.sendQuest("@gre@1129@gre@", 1129);
			client.sendQuest("@gre@1130@gre@", 1130);
			client.sendQuest("@gre@13357@gre@", 13357);
			client.sendQuest("@gre@1131@gre@", 1131);
			client.sendQuest("@gre@11459@gre@", 11459);
		}
		if (barType == 2349) { // Bronze
			if (player.getSkillLevel(SkillType.SMITHING) < 18)
				client.sendQuest("@bla@Plate body1101@bla@", 1101);
			else
				client.sendQuest("@whi@Plate body@whi@", 1101);
			if (player.getSkillLevel(SkillType.SMITHING) < 16) {
				client.sendQuest("@bla@Plate legs1099@bla@", 1099);
				client.sendQuest("@bla@Plate skirt1100@bla@", 1100);
			}
			else {
				client.sendQuest("@whi@Plate legs@whi@", 1099);
				client.sendQuest("@whi@Plate skirt@whi@", 1100);
			}
			if (player.getSkillLevel(SkillType.SMITHING) < 14)
				client.sendQuest("@bla@2 hand sword1088@bla@", 1088);
			else
				client.sendQuest("@whi@2 hand sword@whi@", 1088);
			if (player.getSkillLevel(SkillType.SMITHING) < 13)
				client.sendQuest("@bla@Claws8429@bla@", 8429);
			else
				client.sendQuest("@whi@Claws@whi@", 8429);
			if (player.getSkillLevel(SkillType.SMITHING) < 12)
				client.sendQuest("@bla@Kite shield1105@bla@", 1105);
			else
				client.sendQuest("@whi@Kite shield@whi@", 1105);
			if (player.getSkillLevel(SkillType.SMITHING) < 11)
				client.sendQuest("@bla@Chain body1098@bla@", 1098);
			else
				client.sendQuest("@whi@Chain body@whi@", 1098);
			if (player.getSkillLevel(SkillType.SMITHING) < 10)
				client.sendQuest("@bla@Battle axe1092@bla@", 1092);
			else
				client.sendQuest("@whi@Battle axe@whi@", 1092);
			if (player.getSkillLevel(SkillType.SMITHING) < 9)
				client.sendQuest("@bla@Warhammer1083@bla@", 1083);
			else
				client.sendQuest("@whi@Warhammer@whi@", 1083);
			if (player.getSkillLevel(SkillType.SMITHING) < 8)
				client.sendQuest("@bla@Square shield1104@bla@", 1104);
			else
				client.sendQuest("@whi@Square shield@whi@", 1104);
			if (player.getSkillLevel(SkillType.SMITHING) < 7) {
				client.sendQuest("@bla@Full helm1103@bla@", 1103);
				client.sendQuest("@bla@Throwing knives1106@bla@", 1106);
			}
			else {
				client.sendQuest("@whi@Full helm@whi@", 1103);
				client.sendQuest("@whi@Throwing knives@whi@", 1106);
			}
			if (player.getSkillLevel(SkillType.SMITHING) < 6)
				client.sendQuest("@bla@Long sword1086@bla@", 1086);
			else
				client.sendQuest("@whi@Long sword@whi@", 1086);
			if (player.getSkillLevel(SkillType.SMITHING) < 5) {
				client.sendQuest("@bla@Scimitar1087@bla@", 1087);
				client.sendQuest("@bla@Arrowtips1108@bla@", 1108);
			}
			else {
				client.sendQuest("@whi@Scimitar@whi@", 1087);
				client.sendQuest("@whi@Arrowtips@whi@", 1108);
			}
			if (player.getSkillLevel(SkillType.SMITHING) < 4) {
				client.sendQuest("@bla@Sword1085@bla@", 1085);
				client.sendQuest("@bla@Dart tips1107@bla@", 1107);
				client.sendQuest("@bla@Nails13358@bla@", 13358);
			}
			else {
				client.sendQuest("@whi@Sword@whi@", 1085);
				client.sendQuest("@whi@Dart tips@whi@", 1107);
				client.sendQuest("@whi@Nails@whi@", 13358);
			}
			if (player.getSkillLevel(SkillType.SMITHING) < 3)
				client.sendQuest("@bla@Medium helm1102@bla@", 1102);
			else
				client.sendQuest("@whi@Medium helm@whi@", 1102);
			
			if (player.getSkillLevel(SkillType.SMITHING) < 2)
				client.sendQuest("@bla@Mace1093@bla@", 1093);
			else
				client.sendQuest("@whi@Mace@whi@", 1093);
			
			if (player.getSkillLevel(SkillType.SMITHING) < 1) {
				client.sendQuest("@bla@Dagger1094@bla@", 1094);
				client.sendQuest("@bla@Axe1091@bla@", 1091);
			}
			else {
				client.sendQuest("@whi@Dagger1094@whi@", 1094);
				client.sendQuest("@whi@Axe1091@whi@", 1091);
			}
			client.addItemToSmith(1205, 0, 1119, 1);
			client.addItemToSmith(1351, 0, 1120, 1);
			client.addItemToSmith(1103, 0, 1121, 1);
			client.addItemToSmith(1139, 0, 1122, 1);
			client.addItemToSmith(819, 0, 1123, 10);
			client.addItemToSmith(1277, 1, 1119, 1);
			client.addItemToSmith(1422, 1, 1120, 1);
			client.addItemToSmith(1075, 1, 1121, 1);
			client.addItemToSmith(1155, 1, 1122, 1);
			client.addItemToSmith(39, 1, 1123, 15);
			client.addItemToSmith(1321, 2, 1119, 1);
			client.addItemToSmith(1337, 2, 1120, 1);
			client.addItemToSmith(1087, 2, 1121, 1);
			client.addItemToSmith(1173, 2, 1122, 1);
			client.addItemToSmith(864, 2, 1123, 5);
			client.addItemToSmith(1291, 3, 1119, 1);
			client.addItemToSmith(1375, 3, 1120, 1);
			client.addItemToSmith(1117, 3, 1121, 1);
			client.addItemToSmith(1189, 3, 1122, 1);
			// addItemToSmith(0,3,1123);
			client.sendQuest("one", 1132);
			client.sendQuest("two", 1096);
			client.addItemToSmith(1307, 4, 1119, 1);
			client.addItemToSmith(3095, 4, 1120, 1);
			// addItemToSmith(1307,4,1121);
			client.sendQuest("three", 11459);
			client.sendQuest("for", 11461);
			client.addItemToSmith(4819, 4, 1122, 1);
			// addItemToSmith(1307,4,1123);
			client.sendQuest("five", 1135);
			client.sendQuest("six", 1134);
		}
		/*
		if (barType == 2351) { // Iron
			if (playerLevel[13] < 33) {
				sendQuest("@bla@Plate body@bla@", 1101);
			} else {
				sendQuest("@whi@Plate body@whi@", 1101);
			}
			if (playerLevel[13] < 31) {
				sendQuest("@bla@Plate legs@bla@", 1099);
				sendQuest("@bla@Plate skirt@bla@", 1100);
			} else {
				sendQuest("@whi@Plate legs@whi@", 1099);
				sendQuest("@whi@Plate skirt@whi@", 1100);
			}
			if (playerLevel[13] < 29) {
				sendQuest("@bla@2 hand sword@bla@", 1088);
			} else {
				sendQuest("@whi@2 hand sword@whi@", 1088);
			}
			if (playerLevel[13] < 28) {
				sendQuest("@bla@Claws@bla@", 8429);
			} else {
				sendQuest("@whi@Claws@whi@", 8429);
			}
			if (playerLevel[13] < 27) {
				sendQuest("@bla@Kite shield@bla@", 1105);
			} else {
				sendQuest("@whi@Kite shield@whi@", 1105);
			}
			if (playerLevel[13] < 26) {
				sendQuest("@bla@Chain body@bla@", 1098);
				sendQuest("@bla@Oil lantern frame@bla@", 11461);
			} else {
				sendQuest("@whi@Chain body@whi@", 1098);
				sendQuest("@whi@Oil lantern frame@whi@", 11461);
			}
			if (playerLevel[13] < 25) {
				sendQuest("@bla@Battle axe@bla@", 1092);
			} else {
				sendQuest("@whi@Battle axe@whi@", 1092);
			}
			if (playerLevel[13] < 24) {
				sendQuest("@bla@Warhammer@bla@", 1083);
			} else {
				sendQuest("@whi@Warhammer@whi@", 1083);
			}
			if (playerLevel[13] < 23) {
				sendQuest("@bla@Square shield@bla@", 1104);
			} else {
				sendQuest("@whi@Square shield@whi@", 1104);
			}
			if (playerLevel[13] < 22) {
				sendQuest("@bla@Full helm@bla@", 1103);
				sendQuest("@bla@Throwing knives@bla@", 1106);
			} else {
				sendQuest("@whi@Full helm@whi@", 1103);
				sendQuest("@whi@Throwing knives@whi@", 1106);
			}
			if (playerLevel[13] < 21) {
				sendQuest("@bla@Long sword@bla@", 1086);
			} else {
				sendQuest("@whi@Long sword@whi@", 1086);
			}
			if (playerLevel[13] < 20) {
				sendQuest("@bla@Scimitar@bla@", 1087);
				sendQuest("@bla@Arrowtips@bla@", 1108);
			} else {
				sendQuest("@whi@Scimitar@whi@", 1087);
				sendQuest("@whi@Arrowtips@whi@", 1108);
			}
			if (playerLevel[13] < 19) {
				sendQuest("@bla@Sword@bla@", 1085);
				sendQuest("@bla@Dart tips@bla@", 1107);
				sendQuest("@bla@Nails@bla@", 13358);
			} else {
				sendQuest("@whi@Sword@whi@", 1085);
				sendQuest("@whi@Dart tips@whi@", 1107);
				sendQuest("@whi@Nails@whi@", 13358);
			}
			if (playerLevel[13] < 18) {
				sendQuest("@bla@Medium helm@bla@", 1102);
			} else {
				sendQuest("@whi@Medium helm@whi@", 1102);
			}
			if (playerLevel[13] < 17) {
				sendQuest("@bla@Mace@bla@", 1093);
			} else {
				sendQuest("@whi@Mace@whi@", 1093);
			}
			if (playerLevel[13] < 16) {
				sendQuest("@bla@Axe@bla@", 1091);
			} else {
				sendQuest("@whi@Axe@whi@", 1091);
			}
			if (playerLevel[13] < 15) {
				sendQuest("@bla@Dagger@bla@", 1094);
			} else {
				sendQuest("@whi@Dagger@whi@", 1094);
			}
			addItemToSmith(1203, 0, 1119, 1);
			addItemToSmith(1349, 0, 1120, 1);
			addItemToSmith(1101, 0, 1121, 1);
			addItemToSmith(1137, 0, 1122, 1);
			addItemToSmith(820, 0, 1123, 10);
			addItemToSmith(1279, 1, 1119, 1);
			addItemToSmith(1420, 1, 1120, 1);
			addItemToSmith(1067, 1, 1121, 1);
			addItemToSmith(1153, 1, 1122, 1);
			addItemToSmith(40, 1, 1123, 15);
			addItemToSmith(1323, 2, 1119, 1);
			addItemToSmith(1335, 2, 1120, 1);
			addItemToSmith(1081, 2, 1121, 1);
			addItemToSmith(1175, 2, 1122, 1);
			addItemToSmith(863, 2, 1123, 5);
			addItemToSmith(1293, 3, 1119, 1);
			addItemToSmith(1363, 3, 1120, 1);
			addItemToSmith(1115, 3, 1121, 1);
			addItemToSmith(1191, 3, 1122, 1);
			// addItemToSmith(0,3,1123);
			sendQuest("", 1132);
			sendQuest("", 1096);
			addItemToSmith(1309, 4, 1119, 1);
			addItemToSmith(3096, 4, 1120, 1);
			addItemToSmith(4540, 4, 1121, 1);
			addItemToSmith(4820, 4, 1122, 1);
			// addItemToSmith(1307,4,1123);
			sendQuest("", 1135);
			sendQuest("", 1134);
		}
		if (barType == 2353) { // Steel
			if (playerLevel[13] < 49) {
				sendQuest("@bla@Bullseye lantern@bla@", 11461);
			} else {
				sendQuest("@whi@Bullseye lantern@whi@", 11461);
			}
			if (playerLevel[13] < 48) {
				sendQuest("@bla@Plate body@bla@", 1101);
			} else {
				sendQuest("@whi@Plate body@whi@", 1101);
			}
			if (playerLevel[13] < 46) {
				sendQuest("@bla@Plate legs@bla@", 1099);
				sendQuest("@bla@Plate skirt@bla@", 1100);
			} else {
				sendQuest("@whi@Plate legs@whi@", 1099);
				sendQuest("@whi@Plate skirt@whi@", 1100);
			}
			if (playerLevel[13] < 44) {
				sendQuest("@bla@2 hand sword@bla@", 1088);
			} else {
				sendQuest("@whi@2 hand sword@whi@", 1088);
			}
			if (playerLevel[13] < 43) {
				sendQuest("@bla@Claws@bla@", 8429);
			} else {
				sendQuest("@whi@Claws@whi@", 8429);
			}
			if (playerLevel[13] < 42) {
				sendQuest("@bla@Kite shield@bla@", 1105);
			} else {
				sendQuest("@whi@Kite shield@whi@", 1105);
			}
			if (playerLevel[13] < 41) {
				sendQuest("@bla@Chain body@bla@", 1098);
			} else {
				sendQuest("@whi@Chain body@whi@", 1098);
			}
			if (playerLevel[13] < 40) {
				sendQuest("@bla@Battle axe@bla@", 1092);
			} else {
				sendQuest("@whi@Battle axe@whi@", 1092);
			}
			if (playerLevel[13] < 39) {
				sendQuest("@bla@Warhammer@bla@", 1083);
			} else {
				sendQuest("@whi@Warhammer@whi@", 1083);
			}
			if (playerLevel[13] < 38) {
				sendQuest("@bla@Square shield@bla@", 1104);
			} else {
				sendQuest("@whi@Square shield@whi@", 1104);
			}
			if (playerLevel[13] < 37) {
				sendQuest("@bla@Full helm@bla@", 1103);
				sendQuest("@bla@Throwing knives@bla@", 1106);
			} else {
				sendQuest("@whi@Full helm@whi@", 1103);
				sendQuest("@whi@Throwing knives@whi@", 1106);
			}
			if (playerLevel[13] < 36) {
				sendQuest("@bla@Long sword@bla@", 1086);
				sendQuest("@bla@Studs@bla@", 1134);
			} else {
				sendQuest("@whi@Long sword@whi@", 1086);
				sendQuest("@whi@Studs@whi@", 1134);
			}
			if (playerLevel[13] < 35) {
				sendQuest("@bla@Scimitar@bla@", 1087);
				sendQuest("@bla@Arrowtips@bla@", 1108);
			} else {
				sendQuest("@whi@Scimitar@whi@", 1087);
				sendQuest("@whi@Arrowtips@whi@", 1108);
			}
			if (playerLevel[13] < 34) {
				sendQuest("@bla@Sword@bla@", 1085);
				sendQuest("@bla@Dart tips@bla@", 1107);
				sendQuest("@bla@Nails@bla@", 13358);
			} else {
				sendQuest("@whi@Sword@whi@", 1085);
				sendQuest("@whi@Dart tips@whi@", 1107);
				sendQuest("@whi@Nails@whi@", 13358);
			}
			if (playerLevel[13] < 33) {
				sendQuest("@bla@Medium helm@bla@", 1102);
			} else {
				sendQuest("@whi@Medium helm@whi@", 1102);
			}
			if (playerLevel[13] < 32) {
				sendQuest("@bla@Mace@bla@", 1093);
			} else {
				sendQuest("@whi@Mace@whi@", 1093);
			}
			if (playerLevel[13] < 31) {
				sendQuest("@bla@Axe@bla@", 1091);
			} else {
				sendQuest("@whi@Axe@whi@", 1091);
			}
			if (playerLevel[13] < 30) {
				sendQuest("@bla@Dagger@bla@", 1094);
			} else {
				sendQuest("@whi@Dagger@whi@", 1094);
			}
			addItemToSmith(1207, 0, 1119, 1);
			addItemToSmith(1353, 0, 1120, 1);
			addItemToSmith(1105, 0, 1121, 1);
			addItemToSmith(1141, 0, 1122, 1);
			addItemToSmith(821, 0, 1123, 10);
			addItemToSmith(1281, 1, 1119, 1);
			addItemToSmith(1424, 1, 1120, 1);
			addItemToSmith(1069, 1, 1121, 1);
			addItemToSmith(1157, 1, 1122, 1);
			addItemToSmith(41, 1, 1123, 15);
			addItemToSmith(1325, 2, 1119, 1);
			addItemToSmith(1339, 2, 1120, 1);
			addItemToSmith(1083, 2, 1121, 1);
			addItemToSmith(1177, 2, 1122, 1);
			addItemToSmith(865, 2, 1123, 5);
			addItemToSmith(1295, 3, 1119, 1);
			addItemToSmith(1365, 3, 1120, 1);
			addItemToSmith(1119, 3, 1121, 1);
			addItemToSmith(1193, 3, 1122, 1);
			sendQuest("", 1132);
			sendQuest("", 1096);
			addItemToSmith(1311, 4, 1119, 1);
			addItemToSmith(3097, 4, 1120, 1);
			addItemToSmith(4544, 4, 1121, 1);
			addItemToSmith(1539, 4, 1122, 1);
			addItemToSmith(2370, 4, 1123, 1);
			if (amountOfItem(barType) < 1) {
				sendQuest("@red@1bar@red@", 1135);
			} else {
				sendQuest("@gre@1bar@gre@", 1135);
			}
		}
		if (barType == 2359) { // Mith
			if (playerLevel[13] < 68) {
				sendQuest("@bla@Plate body@bla@", 1101);
			} else {
				sendQuest("@whi@Plate body@whi@", 1101);
			}
			if (playerLevel[13] < 66) {
				sendQuest("@bla@Plate legs@bla@", 1099);
				sendQuest("@bla@Plate skirt@bla@", 1100);
			} else {
				sendQuest("@whi@Plate legs@whi@", 1099);
				sendQuest("@whi@Plate skirt@whi@", 1100);
			}
			if (playerLevel[13] < 64) {
				sendQuest("@bla@2 hand sword@bla@", 1088);
			} else {
				sendQuest("@whi@2 hand sword@whi@", 1088);
			}
			if (playerLevel[13] < 63) {
				sendQuest("@bla@Claws@bla@", 8429);
			} else {
				sendQuest("@whi@Claws@whi@", 8429);
			}
			if (playerLevel[13] < 62) {
				sendQuest("@bla@Kite shield@bla@", 1105);
			} else {
				sendQuest("@whi@Kite shield@whi@", 1105);
			}
			if (playerLevel[13] < 61) {
				sendQuest("@bla@Chain body@bla@", 1098);
			} else {
				sendQuest("@whi@Chain body@whi@", 1098);
			}
			if (playerLevel[13] < 60) {
				sendQuest("@bla@Battle axe@bla@", 1092);
			} else {
				sendQuest("@whi@Battle axe@whi@", 1092);
			}
			if (playerLevel[13] < 59) {
				sendQuest("@bla@Warhammer@bla@", 1083);
			} else {
				sendQuest("@whi@Warhammer@whi@", 1083);
			}
			if (playerLevel[13] < 58) {
				sendQuest("@bla@Square shield@bla@", 1104);
			} else {
				sendQuest("@whi@Square shield@whi@", 1104);
			}
			if (playerLevel[13] < 57) {
				sendQuest("@bla@Full helm@bla@", 1103);
				sendQuest("@bla@Throwing knives@bla@", 1106);
			} else {
				sendQuest("@whi@Full helm@whi@", 1103);
				sendQuest("@whi@Throwing knives@whi@", 1106);
			}
			if (playerLevel[13] < 56) {
				sendQuest("@bla@Long sword@bla@", 1086);
			} else {
				sendQuest("@whi@Long sword@whi@", 1086);
			}
			if (playerLevel[13] < 55) {
				sendQuest("@bla@Scimitar@bla@", 1087);
				sendQuest("@bla@Arrowtips@bla@", 1108);
			} else {
				sendQuest("@whi@Scimitar@whi@", 1087);
				sendQuest("@whi@Arrowtips@whi@", 1108);
			}
			if (playerLevel[13] < 54) {
				sendQuest("@bla@Sword@bla@", 1085);
				sendQuest("@bla@Dart tips@bla@", 1107);
				sendQuest("@bla@Nails@bla@", 13358);
			} else {
				sendQuest("@whi@Sword@whi@", 1085);
				sendQuest("@whi@Dart tips@whi@", 1107);
				sendQuest("@whi@Nails@whi@", 13358);
			}
			if (playerLevel[13] < 53) {
				sendQuest("@bla@Medium helm@bla@", 1102);
			} else {
				sendQuest("@whi@Medium helm@whi@", 1102);
			}
			if (playerLevel[13] < 52) {
				sendQuest("@bla@Mace@bla@", 1093);
			} else {
				sendQuest("@whi@Mace@whi@", 1093);
			}
			if (playerLevel[13] < 51) {
				sendQuest("@bla@Axe@bla@", 1091);
			} else {
				sendQuest("@whi@Axe@whi@", 1091);
			}
			if (playerLevel[13] < 50) {
				sendQuest("@bla@Dagger@bla@", 1094);
			} else {
				sendQuest("@whi@Dagger@whi@", 1094);
			}
			addItemToSmith(1209, 0, 1119, 1);
			addItemToSmith(1355, 0, 1120, 1);
			addItemToSmith(1109, 0, 1121, 1);
			addItemToSmith(1143, 0, 1122, 1);
			addItemToSmith(822, 0, 1123, 10);
			addItemToSmith(1285, 1, 1119, 1);
			addItemToSmith(1355, 1, 1120, 1);
			addItemToSmith(1071, 1, 1121, 1);
			addItemToSmith(1159, 1, 1122, 1);
			addItemToSmith(42, 1, 1123, 15);
			addItemToSmith(1329, 2, 1119, 1);
			addItemToSmith(1343, 2, 1120, 1);
			addItemToSmith(1085, 2, 1121, 1);
			addItemToSmith(1181, 2, 1122, 1);
			addItemToSmith(866, 2, 1123, 5);
			addItemToSmith(1299, 3, 1119, 1);
			addItemToSmith(1369, 3, 1120, 1);
			addItemToSmith(1121, 3, 1121, 1);
			addItemToSmith(1197, 3, 1122, 1);
			// addItemToSmith(0,3,1123);
			sendQuest("", 1132);
			sendQuest("", 1096);
			addItemToSmith(1315, 4, 1119, 1);
			addItemToSmith(3099, 4, 1120, 1);
			// addItemToSmith(4540,4,1121,1);
			addItemToSmith(4822, 4, 1122, 1);
			// addItemToSmith(1307,4,1123);
			sendQuest("", 1135);
			sendQuest("", 1134);
			sendQuest("", 11461);
			sendQuest("", 11459);
		}
		if (barType == 2361) { // Addy
			if (playerLevel[13] < 88) {
				sendQuest("@bla@Plate body@bla@", 1101);
			} else {
				sendQuest("@whi@Plate body@whi@", 1101);
			}
			if (playerLevel[13] < 86) {
				sendQuest("@bla@Plate legs@bla@", 1099);
				sendQuest("@bla@Plate skirt@bla@", 1100);
			} else {
				sendQuest("@whi@Plate legs@whi@", 1099);
				sendQuest("@whi@Plate skirt@whi@", 1100);
			}
			if (playerLevel[13] < 84) {
				sendQuest("@bla@2 hand sword@bla@", 1088);
			} else {
				sendQuest("@whi@2 hand sword@whi@", 1088);
			}
			if (playerLevel[13] < 83) {
				sendQuest("@bla@Claws@bla@", 8429);
			} else {
				sendQuest("@whi@Claws@whi@", 8429);
			}
			if (playerLevel[13] < 82) {
				sendQuest("@bla@Kite shield@bla@", 1105);
			} else {
				sendQuest("@whi@Kite shield@whi@", 1105);
			}
			if (playerLevel[13] < 81) {
				sendQuest("@bla@Chain body@bla@", 1098);
			} else {
				sendQuest("@whi@Chain body@whi@", 1098);
			}
			if (playerLevel[13] < 80) {
				sendQuest("@bla@Battle axe@bla@", 1092);
			} else {
				sendQuest("@whi@Battle axe@whi@", 1092);
			}
			if (playerLevel[13] < 79) {
				sendQuest("@bla@Warhammer@bla@", 1083);
			} else {
				sendQuest("@whi@Warhammer@whi@", 1083);
			}
			if (playerLevel[13] < 78) {
				sendQuest("@bla@Square shield@bla@", 1104);
			} else {
				sendQuest("@whi@Square shield@whi@", 1104);
			}
			if (playerLevel[13] < 77) {
				sendQuest("@bla@Full helm@bla@", 1103);
				sendQuest("@bla@Throwing knives@bla@", 1106);
			} else {
				sendQuest("@whi@Full helm@whi@", 1103);
				sendQuest("@whi@Throwing knives@whi@", 1106);
			}
			if (playerLevel[13] < 76) {
				sendQuest("@bla@Long sword@bla@", 1086);
			} else {
				sendQuest("@whi@Long sword@whi@", 1086);
			}
			if (playerLevel[13] < 75) {
				sendQuest("@bla@Scimitar@bla@", 1087);
				sendQuest("@bla@Arrowtips@bla@", 1108);
			} else {
				sendQuest("@whi@Scimitar@whi@", 1087);
				sendQuest("@whi@Arrowtips@whi@", 1108);
			}
			if (playerLevel[13] < 74) {
				sendQuest("@bla@Sword@bla@", 1085);
				sendQuest("@bla@Dart tips@bla@", 1107);
				sendQuest("@bla@Nails@bla@", 13358);
			} else {
				sendQuest("@whi@Sword@whi@", 1085);
				sendQuest("@whi@Dart tips@whi@", 1107);
				sendQuest("@whi@Nails@whi@", 13358);
			}
			if (playerLevel[13] < 73) {
				sendQuest("@bla@Medium helm@bla@", 1102);
			} else {
				sendQuest("@whi@Medium helm@whi@", 1102);
			}
			if (playerLevel[13] < 72) {
				sendQuest("@bla@Mace@bla@", 1093);
			} else {
				sendQuest("@whi@Mace@whi@", 1093);
			}
			if (playerLevel[13] < 71) {
				sendQuest("@bla@Axe@bla@", 1091);
			} else {
				sendQuest("@whi@Axe@whi@", 1091);
			}
			if (playerLevel[13] < 70) {
				sendQuest("@bla@Dagger@bla@", 1094);
			} else {
				sendQuest("@whi@Dagger@whi@", 1094);
			}
			addItemToSmith(1211, 0, 1119, 1);
			addItemToSmith(1357, 0, 1120, 1);
			addItemToSmith(1111, 0, 1121, 1);
			addItemToSmith(1145, 0, 1122, 1);
			addItemToSmith(823, 0, 1123, 10);
			addItemToSmith(1287, 1, 1119, 1);
			addItemToSmith(1430, 1, 1120, 1);
			addItemToSmith(1073, 1, 1121, 1);
			addItemToSmith(1161, 1, 1122, 1);
			addItemToSmith(43, 1, 1123, 15);
			addItemToSmith(1331, 2, 1119, 1);
			addItemToSmith(1345, 2, 1120, 1);
			addItemToSmith(1091, 2, 1121, 1);
			addItemToSmith(1183, 2, 1122, 1);
			addItemToSmith(867, 2, 1123, 5);
			addItemToSmith(1301, 3, 1119, 1);
			addItemToSmith(1371, 3, 1120, 1);
			addItemToSmith(1123, 3, 1121, 1);
			addItemToSmith(1199, 3, 1122, 1);
			// addItemToSmith(0,3,1123);
			sendQuest("", 1132);
			sendQuest("", 1096);
			addItemToSmith(1317, 4, 1119, 1);
			addItemToSmith(3100, 4, 1120, 1);
			// addItemToSmith(4540,4,1121,1);
			addItemToSmith(4823, 4, 1122, 1);
			// addItemToSmith(1307,4,1123);
			sendQuest("", 1135);
			sendQuest("", 1134);
			sendQuest("", 11461);
			sendQuest("", 11459);
		}
		if (barType == 2363) { // Rune
			if (playerLevel[13] < 99) {
				sendQuest("@bla@Plate body@bla@", 1101);
				sendQuest("@bla@Plate legs@bla@", 1099);
				sendQuest("@bla@Plate skirt@bla@", 1100);
				sendQuest("@bla@2 hand sword@bla@", 1088);
			} else {
				sendQuest("@whi@Plate body@whi@", 1101);
				sendQuest("@whi@Plate legs@whi@", 1099);
				sendQuest("@whi@Plate skirt@whi@", 1100);
				sendQuest("@whi@2 hand sword@whi@", 1088);
			}
			if (playerLevel[13] < 98) {
				sendQuest("@bla@Claws@bla@", 8429);
			} else {
				sendQuest("@whi@Claws@whi@", 8429);
			}
			if (playerLevel[13] < 97) {
				sendQuest("@bla@Kite shield@bla@", 1105);
			} else {
				sendQuest("@whi@Kite shield@whi@", 1105);
			}
			if (playerLevel[13] < 96) {
				sendQuest("@bla@Chain body@bla@", 1098);
			} else {
				sendQuest("@whi@Chain body@whi@", 1098);
			}
			if (playerLevel[13] < 95) {
				sendQuest("@bla@Battle axe@bla@", 1092);
			} else {
				sendQuest("@whi@Battle axe@whi@", 1092);
			}
			if (playerLevel[13] < 94) {
				sendQuest("@bla@Warhammer@bla@", 1083);
			} else {
				sendQuest("@whi@Warhammer@whi@", 1083);
			}
			if (playerLevel[13] < 93) {
				sendQuest("@bla@Square shield@bla@", 1104);
			} else {
				sendQuest("@whi@Square shield@whi@", 1104);
			}
			if (playerLevel[13] < 92) {
				sendQuest("@bla@Full helm@bla@", 1103);
				sendQuest("@bla@Throwing knives@bla@", 1106);
			} else {
				sendQuest("@whi@Full helm@whi@", 1103);
				sendQuest("@whi@Throwing knives@whi@", 1106);
			}
			if (playerLevel[13] < 91) {
				sendQuest("@bla@Long sword@bla@", 1086);
			} else {
				sendQuest("@whi@Long sword@whi@", 1086);
			}
			if (playerLevel[13] < 90) {
				sendQuest("@bla@Scimitar@bla@", 1087);
				sendQuest("@bla@Arrowtips@bla@", 1108);
			} else {
				sendQuest("@whi@Scimitar@whi@", 1087);
				sendQuest("@whi@Arrowtips@whi@", 1108);
			}
			if (playerLevel[13] < 89) {
				sendQuest("@bla@Sword@bla@", 1085);
				sendQuest("@bla@Dart tips@bla@", 1107);
				sendQuest("@bla@Nails@bla@", 13358);
			} else {
				sendQuest("@whi@Sword@whi@", 1085);
				sendQuest("@whi@Dart tips@whi@", 1107);
				sendQuest("@whi@Nails@whi@", 13358);
			}
			if (playerLevel[13] < 88) {
				sendQuest("@bla@Medium helm@bla@", 1102);
			} else {
				sendQuest("@whi@Medium helm@whi@", 1102);
			}
			if (playerLevel[13] < 87) {
				sendQuest("@bla@Mace@bla@", 1093);
			} else {
				sendQuest("@whi@Mace@whi@", 1093);
			}
			if (playerLevel[13] < 86) {
				sendQuest("@bla@Axe@bla@", 1091);
			} else {
				sendQuest("@whi@Axe@whi@", 1091);
			}
			if (playerLevel[13] < 85) {
				sendQuest("@bla@Dagger@bla@", 1094);
			} else {
				sendQuest("@whi@Dagger@whi@", 1094);
			}
			addItemToSmith(1213, 0, 1119, 1);
			addItemToSmith(1359, 0, 1120, 1);
			addItemToSmith(1113, 0, 1121, 1);
			addItemToSmith(1147, 0, 1122, 1);
			addItemToSmith(824, 0, 1123, 10);
			addItemToSmith(1289, 1, 1119, 1);
			addItemToSmith(1432, 1, 1120, 1);
			addItemToSmith(1079, 1, 1121, 1);
			addItemToSmith(1163, 1, 1122, 1);
			addItemToSmith(44, 1, 1123, 15);
			addItemToSmith(1333, 2, 1119, 1);
			addItemToSmith(1347, 2, 1120, 1);
			addItemToSmith(1093, 2, 1121, 1);
			addItemToSmith(1185, 2, 1122, 1);
			addItemToSmith(868, 2, 1123, 5);
			addItemToSmith(1303, 3, 1119, 1);
			addItemToSmith(1373, 3, 1120, 1);
			addItemToSmith(1127, 3, 1121, 1);
			addItemToSmith(1201, 3, 1122, 1);
			// addItemToSmith(0,3,1123);
			sendQuest("", 1132);
			sendQuest("", 1096);
			addItemToSmith(1319, 4, 1119, 1);
			addItemToSmith(3101, 4, 1120, 1);
			// addItemToSmith(4540,4,1121,1);
			addItemToSmith(4824, 4, 1122, 1);
			// addItemToSmith(1307,4,1123);
			sendQuest("", 1135);
			sendQuest("", 1134);
			sendQuest("", 11461);
			sendQuest("", 11459);
		}
		
	}
	*/
	
	public static SmithingService getInstance() { return instance; }
	
	private class SmithingPos {
		private int slot;
		private int column;
		private int name;
		private int bar;
		
		public SmithingPos(int slot, int column, int name, int bar) {
			this.slot = slot;
			this.column = column;
			this.name = name;
			this.bar = bar;
		}
		
		public int getSlot() { return slot; }
		public int getColumn() { return column; }
		public int getName() { return name; }
		public int getBar() { return bar; }
	}
	
	private class SmithingItem {
		private int reqBar;
		private int reqCount;
		private int reqLevel;
		private int item;
		private String name;
		private int count;
		private int position;
		
		public SmithingItem(int reqBar, int reqCount, int reqLevel, int item, String name, int count, int position) {
			this.reqBar = reqBar;
			this.reqCount = reqCount;
			this.reqLevel = reqLevel;
			this.item = item;
			this.name = name;
			this.count = count;
			this.position = position;
		}
		
		public int getReqBar() { return reqBar; }
		public int getReqCount() { return reqCount; }
		public int getReqLevel() { return reqLevel; }
		public int getItem() { return item; }
		public String getName() { return name; }
		public int getCount() { return count; }
		public int getPosition() { return position; }
	}
}